/* ---------------------------------------------------------------------------
   (c) Copyright 2006 ActivIdentity, Inc.
   All Rights Reserved.

   This program is an unpublished copyrighted work which is proprietary
   to ActivIdentity. This computer program includes Confidential,
   Proprietary Information and is a Trade Secret of ActivIdentity.
   Any use, disclosure, modification and/or reproduction is prohibited
   unless authorized in writing by ActivIdentity.

   WARNING:  Unauthorized reproduction of this program as well as
   unauthorized preparation of derivative works based upon the
   program or distribution of copies by sale, rental, lease or
   lending are violations of federal copyright laws and state trade
   secret laws, punishable by civil and criminal penalties.
   --------------------------------------------------------------------------- */

/* ---------------------------------------------------------------------------
   Disclaimer

   ACTIVIDENTITY MAKES NO REPRESENTATIONS OR WARRANTIES WITH RESPECT TO THE 
   CONTENTS OR USE OF THIS SOFTWARE, AND SPECIFICALLY DISCLAIMS ANY EXPRESS
   OR IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR
   PURPOSE. Further, ActivIdentity reserves the right to revise this software
   and to make changes to its content, at any time, without obligation to
   notify any person or entity of such revisions or changes.
   --------------------------------------------------------------------------- */

#ifndef __IMPEXPAPI_H__
#define __IMPEXPAPI_H__

#ifdef __cplusplus
extern "C" {
#endif

/* Parameters */
#define IN
#define OUT
#define INOUT

#ifdef WIN32
/* Windows standard files and definitions */
	#include <windows.h>
#else
/* Unix standard files and definitions */
	#define CALLBACK
	#define TRUE 1
	#define FALSE 0
#endif
#include <stdio.h>
#include <sys/stat.h>

#include "actoken.h"
#include "acm.h"
#include "sdbapi.h"


/***************/
/** CONSTANTS **/
/***************/

/* open mode */
enum
{
	ACIE_READ	= 1,/* open for reading flag */
	ACIE_WRITE	= 2	/* open for writing flag (ONLY for generic and kernel format) */
};

/* Formats */
enum
{
	ACIE_FMT_TOK		=1,	/* generic format .tok */
	ACIE_FMT_AK			=2,	/* kernel format .ak */
	ACIE_FMT_DEV		=3,	/* manufacturing format (.dev, .evl)*/
	ACIE_FMT_APD		=4	/* ActivIdentity Personalization Data format .apd */
};

enum
{
	ACIE_ERR_NO_ERROR		=0,	/* no error */
	ACIE_ERR_BAD_PARAM		=1,	/* bad parameter(s) */
	ACIE_ERR_UNKNOWNFILE	=2,	/* bad file path or unknown file, 
									or lacking .evl file in manufacturing format */
	ACIE_ERR_ACCESSFILE		=4,	/* open mode and file open rights incompatibility 
									or bad key */
	ACIE_ERR_BADFORMAT		=5,	/* bad format or format not compliant with 
									extension in filename or access mode */
	ACIE_ERR_NOMORE_DEVICE	=6,	/* end of file */
	ACIE_ERR_INOUT			=7,	/* input/output error */
	ACIE_ERR_MEMALLOC		=8,	/* memory allocation problem */
	ACIE_ERR_INTERNAL_ERROR	=9, /* internal error */
	ACIE_ERR_BAD_FILE_CONT	=10, /* bad file content */
	ACIE_ERR_INCOMPATIBLE_FUNC	=11 /* file content incompatible with call */
};

/**********************/
/** TYPE DEFINITIONS **/
/**********************/
/*********************************************************************
* Structures
*********************************************************************/
/* Header */
typedef struct
{
	int		nHMagic;			/* Header: magic number */
	char	aHVersion	[20+4];	/* Header: dll version */
	char	aHDate		[19+1];	/* Header: date	*/
	char	aHFrom		[40+1];	/* Header: origin of file */
	char	aHField1	[40+1];	/* Encryption type : DES/EDE */
	char	aHField2	[40+1];	/* Header: EK for envelop (transport Key) */
	char	aHField3	[40+1];	/* Header: field 3 */
	char	aHField4	[40+1];	/* Header: field 4 */
	char	aHField5	[40+1];	/* Header: field 5 */
	int		nHRecCnt;			/* Header: record counter */
	char	aHCyphT		[10+1]; /*  */
	char	aHMacGT		[10+1];	/* Header: global cryptographic type */
	char	aHMacRT		[10+1];	/* Header: block cryptographic type */
} strHeader;


typedef struct {
	FILE		*pFile;			/* Pointer on the opened file */
	char		szSessionKey[32+1];	/* Transport key */
 	char		szMacKey[32+1];		/* RFU */
	strHeader	sHeader;		/* Header */
	int			nRecordCnt;		/* Counter of records */
	char		szRec[10000];
} strContext;

/* input/output data structure */
typedef struct 
{
	strToken sTok;
	strSlot sSlot[ACM_NBMAX_SLOT];
} StrTokenSlot;

typedef struct 
{
	strToken sTok;
	strExtSlot sSlot[ACM_NBMAX_SLOT];
} StrTokenExtSlot;

/* data file parameters */
typedef struct 
{
	char szEncryptKey[32 + 1];		/* eventual encryption key */
	int nOpenMode;			/* open mode read/write */
	int nFormat;			/* file format */
	strContext strCtx;		/* file context structure */
} TokenSetParam;


/***************/
/** FUNCTIONS **/
/***************/

/* Open file function */
int CALLBACK acTokenSetOpenFile(
	INOUT TokenSetParam *psParam,
	IN const char *pszFileName,
	OUT FILE **ppFile);

/* Close file function */
int CALLBACK acTokenSetCloseFile(
	INOUT TokenSetParam *psParam,
	IN FILE *pFile);

/* Read the next record in the file */
int CALLBACK acTokenSetReadFileNext(
	INOUT TokenSetParam *psParam,
	IN FILE *pFile,
	OUT StrTokenSlot *pstrData);

/* Write the next record in the file */
int CALLBACK acTokenSetWriteFileNext(
	INOUT TokenSetParam *psParam,
	IN FILE *pFile,
	IN StrTokenSlot *pstrData);

/* Read the next record in the file */
int CALLBACK acTokenSetReadFileNextExt(
	INOUT TokenSetParam *psParam,
	IN FILE *pFile,
	OUT StrTokenExtSlot *pstrData);

int CALLBACK acTokenSetWriteFileNextExt(
	INOUT TokenSetParam *psParam,
	IN FILE *pFile,
	IN StrTokenExtSlot *pstrData);

/* Get API version informations */
void CALLBACK acTokenSetGetVersion(
        OUT char *pszVersion,
        OUT unsigned int *plVersion,
        OUT char *pszCopyright, 
        OUT char *pszComment);

/* Write the next SDB record in the file */
int CALLBACK acSdbWriteFileNext(
		INOUT TokenSetParam *psParam,
		IN PSDB pSdb,
		IN char *pszSdbKey);

#ifdef __cplusplus
}
#endif

#endif /* __IMPEXPAPI_H__ */
